<?php  
/**
* 
*/
class BorrowingSystemController extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('borrowingSystem_model');
		$this->load->model('utility_model');
		
	}

	public function index(){

	}

	public function popup(){
		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/popup");
		$this->load->view("include/footer");
	}

	public function find(){
	
		if ($this->input->post('btnFind') != null) {
			$condition = '';
			if ($this->input->post('code') != "") {
				$condition .= "AND file_caseid LIKE '%".$this->input->post('code')."%' ";
			}

			if ($this->input->post('name') != "") {
				$condition .= "AND file_casename LIKE '%".$this->input->post('name')."%' ";
			}

			if($this->input->post('status') == "all"){
				$condition .= "";
			}
			else if($this->input->post('status') == "free"){
				$condition .= "AND having sum(convert(int,case_file.file_brstatus)) = 0";
			}
			else if($this->input->post('status') == "busy"){
				$condition .= "AND having sum(convert(int,case_file.file_brstatus)) <> 0 ";
			}

			$data['rsFindCase'] = $this->borrowingSystem_model->findCase($condition);

			$this->load->view("include/header");
			$this->load->view("BorrowingSystem/find",$data);
			$this->load->view("include/footer");
			return;
		}

		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/find");
		$this->load->view("include/footer");
	}


	public function borrowFile(){
		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/borrowFile");
		$this->load->view("include/footer");
	}

	public function returnFile(){
		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/returnFile");
		$this->load->view("include/footer");
	}

	public function manageFile(){
		
		$rs = $this->borrowingSystem_model->getListCaseFiles();

		$data['jsonCaseFile'] = json_encode($this->utility_model->changeToUTF8($rs));

		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/manage",$data);
		$this->load->view("include/footer");

	}

	public function change_status(){
		$status = $this->input->post('status');

		$newStatus = array();
		if($status == "Y")
			$newStatus = array('file_status'=>"N");
		else{
			$newStatus = array('file_status'=>"Y");
		}
		
		echo $this->borrowingSystem_model->change_status($this->input->post('id'),$newStatus);
	}

	public function create_casefile(){

		$listGroupFile = $this->borrowingSystem_model->getListGroup();


		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/createAndEdit");
		$this->load->view("include/footer");
	}

	public function getListCase(){
		$id = $this->input->post('id');
		echo json_encode($this->utility_model->changeToUTF8($this->borrowingSystem_model->getCaseFileWithCaseID($id)));
	}

	public function edit_casefile($id){
		$data['idItem'] = $id;
		$data['rs'] = $this->borrowingSystem_model->getCaseFileWithCaseID($id);
		$data['jsondata'] = json_encode($this->utility_model->changeToUTF8($data['rs']));

		$listGroupFile = $this->borrowingSystem_model->getListGroup();
		$textListGroupFile = array();
		foreach ($listGroupFile as $key => $value) {

			array_push($textListGroupFile, $this->utility_model->changeToUTF8($value));	

		}
		$data['textListGroupFile'] = $textListGroupFile;
		

		$this->load->view("include/header");
		$this->load->view("BorrowingSystem/createAndEdit",$data);
		$this->load->view("include/footer");
	}

	public function sub_addFileDoc(){
		$newFile = $this->input->post('newfile');

		if($this->borrowingSystem_model->checkIdFileCase($newFile[2]) > 0){
			echo 'found';
		}
		else {
			//echo 'missing';

			$arrNew = array('file_id' => $this->input->post('fid'),
											'document_name' => iconv('UTF-8', 'TIS-620', $newFile[0]),
											'document_group' => iconv('UTF-8', 'TIS-620', $newFile[1]),	
											'index_code' => $newFile[2],
											'document_detail' => iconv('UTF-8', 'TIS-620', $newFile[3])
										);

			$this->borrowingSystem_model->add_filecase($arrNew);
		}
		
		//$arrayName = array('' => , );
	}

	public function delFileCase(){
		$id = $this->input->post('id');
		echo $this->borrowingSystem_model->del_filecase($id);
	}

	public function addCase(){
		// echo $this->input->post('case_id');
		// echo $this->input->post('case_name');

		if ($this->borrowingSystem_model->checkIdCase($this->input->post('case_id')) > 0) {
			echo "found";
		}else{
			//echo "notfound";

      $session_data = $this->session->userdata('userdata');
      $userInfo = $session_data['UserInfo'];
      //echo $userInfo['title'].$userInfo['emp_name']."  ".$userInfo['emp_lastname'];
   

			// $this->amcamdb = $this->load->database('amcamdb', TRUE);
			// $data_oid = $this->amcamdb->query('SELECT pet_case_data_oid FROM pet_case_data WHERE pet_case_data_id = "'.$this->input->post('case_id').'"');
			
			$newObj = array('file_status' => 'Y',
											'file_brstatus' => 'N' ,
											'file_caseid' => $this->input->post('case_id'),
											'file_casename' => $this->input->post('case_name'),
											'file_createdate' => date('Y-m-d H:i:s'),
											'file_createby' => $userInfo['emp_user']
											//'file_pet_case_data_oid' => $data_oid 
											);

			echo $this->borrowingSystem_model->createCase($newObj);

		}


	}

	// public function editFileCase($id){

	// 	if ($this->input->post('btnEdit') != null) {
	// 		$this->load->library('form_validation');

	// 		$this->form_validation->set_rules('case_id', 'รหัสคดี', 'trim|required');
	// 		$this->form_validation->set_rules('case_name', 'รายคดี/ชื่อคดี', 'trim|required|');
	// 		$this->form_validation->set_rules('file_number', 'แฟ้มสำนวนคดีที่', 'trim|required');
	// 		$this->form_validation->set_rules('filename', 'ชื่อแฟ้ม', 'trim|required|');
	// 		// $this->form_validation->set_rules('file_casedetail', 'รายละเอียด', 'trim|required|');

	// 		$this->form_validation->set_message('required','กรุณาใส่ %s ');

	// 		if ($this->form_validation->run() == FALSE)
	// 		{

	// 		}
	// 		else
	// 		{
	// 			$fileUpdate = array( 
	// 				'file_caseid' =>  iconv('UTF-8', 'TIS-620', $this->input->post('case_id')),
	// 				'file_casename' => iconv('UTF-8', 'TIS-620', $this->input->post('case_name')),
	// 				'file_number' => iconv('UTF-8', 'TIS-620', $this->input->post('file_number')),
	// 				'file_name' => iconv('UTF-8', 'TIS-620', $this->input->post('filename')),
	// 				'file_casedetail' => iconv('UTF-8', 'TIS-620', $this->input->post('file_casedetail'))
	// 			);

	// 			$this->borrowingSystem_model->update($id,$fileUpdate);

	// 			redirect('BorrowingSystemController/manageFile','refresh');
	// 			exit();
	// 		}
	// 	}

	// 	$data['rsCaseFile'] = $this->borrowingSystem_model->getCaseFile($id);

	// 	$this->load->view("include/header");
	// 	$this->load->view("BorrowingSystem/edit",$data);
	// 	$this->load->view("include/footer");
	// }

	// public function deleteFileCase($id){
	// 	$this->borrowingSystem_model->delete($id);
	// 	redirect('BorrowingSystemController/manageFile','refresh');
	// }
}
?>